<?php

/*
	Class: CommerceUser_Additional

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>
*/
class CommerceUser_Additional {

	/* Group: Instance Methods */

	/*
		Function: process
	*/
	function process($params) {
		
		$request = new Cts_Request($params['request']);
		$user = $params['user'];
		$errors = $params['errors'];
		$users_table = new Commerce_Users();
		$old_users_table = new Users();
		$old_user = $old_users_table->fetchByUsername($user['username']);
		$userwhere = $users_table->getAdapter()->quoteInto('username = ?', $user['username']);
		$commerce_user = $users_table->fetchRow($userwhere);
		if (is_null($commerce_user)) {
			$commerce_user = $users_table->createRow();
			$commerce_user->username = $user['username'];
		}

		$commerce = new Commerce_Magento_Membership();

		// check that we're not trying to use an email address that is already assigned to a commerce customer
		if (!is_null($commerce->getCustomerByEmail($user['email'])) and $user['email'] != $old_user->email) {
			// TODO - pass in locale code as a param
			$errors[] = Cts_Translate::translate("en-US", "commerce", "%s is already in use.", $user['email']);
		}

		if (count($errors) == 0) {

			// check for commerce customer
			$customer_id = $commerce->getCustomerIdByUsername($user['username']);

			if (is_null($customer_id)) {
				// create new customer
				$temp_pass = substr(md5(time()), 0, 10);
				$customer_id = $commerce->createCustomer($user['first_name'], $user['last_name'], $user['email'], $temp_pass);
				$commerce_user->commerce_customer_id = $customer_id;
			} else {
				// already have a customer, update
				if ($user['password'] != "") {
					$password = $user['password'];
				} else {
					$password = null;
				}
				$commerce->updateCustomer($customer_id, $user['first_name'], $user['last_name'], $user['email'], $password);
			}

			// validate our address ID, if not, clear it.

			$address_list = $commerce->getAddressList($customer_id);
			$address_ids = array();
			foreach ($address_list as $address) {
				$address_ids[] = $address['customer_id']; // TODO This is wrong in the API, should  be fixed.
			}

			if (!in_array($commerce_user->commerce_address_id, $address_ids)) {
				$commerce_user->commerce_address_id = null;
			}

			// check to see if we have a commerce address id
			if (!is_null($commerce_user->commerce_address_id)) {
				$address = array(
					'first_name'	=> $user['first_name'],
				    'last_name'   	=> $user['last_name'],
					'country' 		=> $user['country_code'],
					'address_1'  	=> $commerce_user->address_1,
				    'address_2'		=> $commerce_user->address_2,
					'city'       	=> $commerce_user->city,
				    'state'			=> $commerce_user->state,
				    'phone'  		=> $commerce_user->phone,
				    'zip'   		=> $commerce_user->zip,
				);
				$commerce->updateAddress($commerce_user->commerce_address_id, $address);
			} else {
				// create new address if we have enough info to do so
				$address = array(
					'first_name'	=> $user['first_name'],
				    'last_name'   	=> $user['last_name'],
					'country' 		=> $user['country_code'],
					'address_1'  	=> $commerce_user->address_1,
				    'address_2'		=> $commerce_user->address_2,
					'city'       	=> $commerce_user->city,
				    'state'			=> $commerce_user->state,
				    'phone'  		=> $commerce_user->phone,
				    'zip'   		=> $commerce_user->zip,
				);

				$commerce_user->commerce_address_id = $commerce->createAddress($customer_id, $address);
			}
			$commerce_user->save();
		}

		$commerce_user = $commerce_user->toArray();
		foreach ($commerce_user as $key => $val) {
			$user[$key] = $val;
		}

		$params['user'] = $user;
		$params['errors'] = $errors;

		return $params;
	}

	/*
		Function: fill
	*/
	function fill($params) {
		$user = $params['user'];
		$users_table = new Commerce_Users();
		$userwhere = $users_table->getAdapter()->quoteInto('username = ?', $user['username']);
		$commerce_user = $users_table->fetchRow($userwhere);
		if (!is_null($commerce_user)) {
			$commerce_user = $commerce_user->toArray();
			foreach ($commerce_user as $key => $val) {
				$user[$key] = $val;
			}
		}
		return $params;
	}

}
